/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.UserMapper;
import cn.qidisoft.edu.hzjt.model.User;
import cn.qidisoft.edu.hzjt.service.IUserService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * @author hhl
 * @version $Id: IUserServiceImpl.java, v 0.1 2018年9月11日 下午7:49:18 hhl Exp $
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService {
  protected static final LoggerAdapter LOGGER =
      LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);
  @Autowired
  private UserMapper userMapper;

  /**
   * @see cn.qidisoft.microClass.resource.service.IUserService#login(com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.User)
   */
  @Override
  public User login(User user) {
    try {
      return userMapper.queryByUserNamePassword(user);
    } catch (DBException e) {
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.microClass.resource.service.IUserService#queryPageList(cn.qidisoft.microClass.resource.model.basis.User,
   *      org.apache.ibatis.session.RowBounds)
   */
  @Override
  public PaginationBean<User> queryPageList(User user, Page page) {
    try {
      int c = userMapper.queryPageListcount(user);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<User> pageHolder = new PaginationBean<User>(page, totalRecords);
      List<User> resultList = userMapper.queryPageList(user, new RowBounds(
          (pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(), pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.microClass.resource.service.IUserService#queryList(cn.qidisoft.microClass.resource.model.basis.User)
   */
  @Override
  public List<User> queryList(User user) {
    try {
      return userMapper.queryList(user);
    } catch (DBException e) {
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.microClass.resource.service.IUserService#queryByPK(cn.qidisoft.microClass.resource.model.basis.User)
   */
  @Override
  public User queryByPK(User user) {
    try {
      return userMapper.queryByPK(user);
    } catch (DBException e) {
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.microClass.resource.service.IUserService#add(cn.qidisoft.microClass.resource.model.basis.User)
   */
  @Override
  @Transactional
  public int add(User user) {
    try {
      return userMapper.add(user);
    } catch (DBException e) {
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.microClass.resource.service.IUserService#update(cn.qidisoft.microClass.resource.model.basis.User)
   */
  @Override
  @Transactional
  public int update(User user) {
    try {
      return userMapper.update(user);
    } catch (DBException e) {
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.microClass.resource.service.IUserService#delete(cn.qidisoft.microClass.resource.model.basis.User)
   */
  @Override
  @Transactional
  public int delete(User user) {
    try {
      return userMapper.delete(user);
    } catch (DBException e) {
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.microClass.resource.service.IUserService#validation(cn.qidisoft.microClass.resource.model.basis.User)
   */
  @Override
  public int validation(User user) {
    try {
      return userMapper.validation(user);
    } catch (DBException e) {
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(e.getMessage(), e);
      throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(), e);
    }
  }

}
